`
https://leetcode.cn/problems/count-subarrays-where-max-element-appears-at-least-k-times/
`

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var countSubarrays = function (nums, k) {
  const n = nums.length
  const maxVal = Math.max(...nums)
  let res = 0
  let count = 0
  let left = 0, right = 0

  while (right < n) {
    const n1 = nums[right++]
    if (n1 === maxVal) count++

    // 关注 left - 1 往前的合法性
    while (count === k) {
      const n2 = nums[left++]
      if (n2 === maxVal) count--
    }

    // 刚好有 left 个
    res += left
  }

  return res
};